package com.iyjhsoc.game.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iyjhsoc.game.ENUM.ConstantENUM;
import com.iyjhsoc.game.dao.PlayerGameTeamDAO;
import com.iyjhsoc.game.dao.PlayerTeamDAO;
import com.iyjhsoc.game.domain.dto.FirstPlayerDTO;
import com.iyjhsoc.game.domain.dto.PlayerDTO;
import com.iyjhsoc.game.domain.dto.PlayerTeamDTO;
import com.iyjhsoc.game.domain.entity.PlayerTeamDO;
import com.iyjhsoc.game.domain.response.Response;
import com.iyjhsoc.game.service.PlayerTeamService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PlayerTeamServiceImpl extends ServiceImpl<PlayerTeamDAO, PlayerTeamDO> implements PlayerTeamService {
    @Autowired
    PlayerTeamDAO playerTeamDAO;

    @Override
    public Response queryFirstPlayer(FirstPlayerDTO dto) {
        QueryWrapper<PlayerTeamDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",0);
        queryWrapper.eq("is_entry",0);
        queryWrapper.eq("is_first",1);
        queryWrapper.eq("team_id",dto.getTeamId());
        List<PlayerTeamDO> playerTeamDOS = playerTeamDAO.selectList(queryWrapper);
        FirstPlayerDTO firstPlayerDTO = new FirstPlayerDTO();
        for(PlayerTeamDO playerTeamDO: playerTeamDOS){
            if("1".equals(playerTeamDO.getPosition())){
                firstPlayerDTO.setTopId(playerTeamDO.getPlayerId());
            } else  if("2".equals(playerTeamDO.getPosition())){
                firstPlayerDTO.setJugId(playerTeamDO.getPlayerId());
            } else if("3".equals(playerTeamDO.getPosition())){
                firstPlayerDTO.setMidId(playerTeamDO.getPlayerId());
            } else if("4".equals(playerTeamDO.getPosition())){
                firstPlayerDTO.setBomId(playerTeamDO.getPlayerId());
            } else if("5".equals(playerTeamDO.getPosition())){
                firstPlayerDTO.setAssId(playerTeamDO.getPlayerId());
            }
        }
        return Response.SUCCESS(firstPlayerDTO);
    }

    @Override
    public Response insertFirstPlayer(FirstPlayerDTO dto) {
        PlayerTeamDO top = new PlayerTeamDO();
        top.setIsFirst(1);
        top.setPlayerId(dto.getTopId());
        top.setIsEntry(0);
        top.setTeamId(dto.getTeamId());
        top.setIsDelete(0);
        top.setPosition("1");
        playerTeamDAO.insert(top);
        PlayerTeamDO jug = new PlayerTeamDO();
        jug.setIsFirst(1);
        jug.setPlayerId(dto.getJugId());
        jug.setIsEntry(0);
        jug.setTeamId(dto.getTeamId());
        jug.setIsDelete(0);
        jug.setPosition("2");
        playerTeamDAO.insert(jug);
        PlayerTeamDO mid = new PlayerTeamDO();
        mid.setIsFirst(1);
        mid.setPlayerId(dto.getMidId());
        mid.setIsEntry(0);
        mid.setTeamId(dto.getTeamId());
        mid.setIsDelete(0);
        mid.setPosition("3");
        playerTeamDAO.insert(mid);
        PlayerTeamDO bom = new PlayerTeamDO();
        bom.setIsFirst(1);
        bom.setPlayerId(dto.getBomId());
        bom.setIsEntry(0);
        bom.setTeamId(dto.getTeamId());
        bom.setIsDelete(0);
        bom.setPosition("4");
        playerTeamDAO.insert(bom);
        PlayerTeamDO ass = new PlayerTeamDO();
        ass.setIsFirst(1);
        ass.setPlayerId(dto.getAssId());
        ass.setIsEntry(0);
        ass.setTeamId(dto.getTeamId());
        ass.setIsDelete(0);
        ass.setPosition("5");
        playerTeamDAO.insert(ass);
        return Response.SUCCESS(ConstantENUM.SUCCESS);
    }

    @Override
    public Response updateFirstPlayer(FirstPlayerDTO dto) {
        QueryWrapper<PlayerTeamDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("team_id",dto.getTeamId());
        queryWrapper.eq("is_first",1);
        int delete = playerTeamDAO.delete(queryWrapper);
        Response response = insertFirstPlayer(dto);
        return response;
    }

    @Override
    public Response getPlayerList(FirstPlayerDTO dto) {
        List<PlayerTeamDTO> playerDTOS = playerTeamDAO.queryList(dto);
        return Response.SUCCESS(playerDTOS);
    }

    @Override
    public Response insertSubstitute(List<PlayerTeamDTO> playerTeamDTOS) {
        for(PlayerTeamDTO playerTeamDTO: playerTeamDTOS) {
            PlayerTeamDO playerTeamDO = new PlayerTeamDO();
            BeanUtils.copyProperties(playerTeamDTO,playerTeamDO);
            playerTeamDAO.insert(playerTeamDO);
        }
        return Response.SUCCESS("新增成功");
    }

    @Override
    public Response getSubstitute(PlayerDTO playerDTO) {
        QueryWrapper<PlayerTeamDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("team_id",playerDTO.getTeamId());
        queryWrapper.eq("is_delete","0");
        queryWrapper.eq("is_entry","0");
        queryWrapper.eq("is_first","0");
        List<PlayerTeamDO> playerTeamDOS = playerTeamDAO.selectList(queryWrapper);
        return Response.SUCCESS(playerTeamDOS);
    }

    @Override
    public Response delSubstitute(PlayerTeamDO playerTeamDO) {
        int i = playerTeamDAO.deleteById(playerTeamDO.getId());
        if(i == 1){
            return Response.SUCCESS("删除成功");
        }
        return Response.ERROR("删除失败");
    }
}
